const { User, Role, Store } = require('../common/mysql.js')
const { Op } = require('sequelize')

module.exports = {
    async getStoreUserList(store_id) {
        return await User.findAll({
            include: [
                {
                    model: Role,
                    where: {
                        role: {
                            [Op.eq]: 'EDP'
                        }
                    },
                    attributes: []
                }
            ],
            attributes: ['id', 'name'],
            where: {
                store_id: {
                    [Op.eq]: store_id
                }
            }
        })
    },
    async getUserById(id) {
        return await User.findByPk(id, {
            attributes: ['name', 'tel']
        })
    },
    async getUserByIdForPassword(id) {
        return await User.findByPk(id, {
            attributes: ['enable', 'password']
        })
    },
    async getUserByName(username) {
        return await User.findOne({
            include: [
                {
                    model: Role
                },
                {
                    model: Store
                }
            ],
            where: {
                username: {
                    [Op.eq]: username
                },
                enable: {
                    [Op.eq]: true
                }
            }
        })
    },
    async updatePassword(password, id) {
        await User.update(
            {
                password
            },
            {
                fields: ['password'],
                where: {
                    id: {
                        [Op.eq]: id
                    }
                }
            }
        )
    },
    async updateInformation(user, id) {
        await User.update(user, {
            fields: ['name', 'tel'],
            where: {
                id: {
                    [Op.eq]: id
                }
            }
        })
    }
}
